
package com.ruipengkj.view.memberView;
/**
 * 说明：
 * 会员级别管理
 * @Author：孙航
 */
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.table.TableRowSorter;

import com.ruipengkj.bean.TShopMemberLevel;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageIndex;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.page.PageDelegale;
import com.ruipengkj.component.page.PagingComponent;
import com.ruipengkj.model.MemberLevelModel;
import com.ruipengkj.service.TShopMemberLevelService;
import com.ruipengkj.view.base.BasePagePanel;

public class MemberLevelManagePanel extends BasePagePanel<TShopMemberLevel> implements PageDelegale ,Runnable{

	private static final long serialVersionUID = 1L;
	
	private JButton queryButton; //查询
	private JButton addButton; //增加会员级别
	private JButton updateButton; //修改会员级别
	private JButton delButton; //删除会员级别
	
	private JTable table;
	private JScrollPane tableJsp;
	private MemberLevelModel memberLevelModel;
	
	private PagingComponent pagingComponent;//翻页组件
	private PageView<TShopMemberLevel> pageView;//翻页主键
	private TShopMemberLevelService tShopMemberLevelService;//仓库操作对象
	private MemberLevelDialog memberLevelDialog;//添加或修改仓库对话框
	
	public MemberLevelManagePanel() {
		setLayout(new BorderLayout());
		init();
		initListener();
		tShopMemberLevelService=BuildSpringApplication.getInstance().getTShopMemberLevelServiceImpl();
	}
	
	private JPanel initTop() {
		JPanel top_panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		
//		storageID = new JLabelJTextFieldComponent("仓库编号：", 20);
//		storageName= new JLabelJTextFieldComponent("仓库名称：", 20);
		queryButton = new JButton("查询");
		
//		top_panel.add(storageID);
//		top_panel.add(storageName);
		top_panel.add(queryButton);
		return top_panel;
	}
	
	private void init() {
		pageView = new PageView<TShopMemberLevel>();
		memberLevelModel = new MemberLevelModel();
		table=uiFactory.createJTable(memberLevelModel, null, this, 30, JLabel.CENTER);
		tableJsp=new JScrollPane(table);
		pagingComponent=new PagingComponent();
		pagingComponent.setPageDelegale(this);
		
		JPanel gardPanel=new JPanel(new GridLayout(2,1));
		JPanel jp1=new JPanel(new FlowLayout(FlowLayout.RIGHT));
		addButton = new JButton("增加会员级别(Ctrl+A)");
		updateButton = new JButton("修改会员级别(Ctrl+M)");
		delButton = new JButton("删除会员级别(Ctrl+D)");
		jp1.add(addButton);
		jp1.add(updateButton);
		jp1.add(delButton);
		gardPanel.add(pagingComponent);
		gardPanel.add(jp1);
		this.setButtonEnabled(false);
		
		this.add(initTop(),BorderLayout.NORTH);
		this.add(tableJsp,BorderLayout.CENTER);
		this.add(gardPanel,BorderLayout.SOUTH);
		//排序
		TableRowSorter<MemberLevelModel> tableRowSorter = new TableRowSorter<MemberLevelModel>(memberLevelModel);  
		table.setRowSorter(tableRowSorter);
		this.termQuery(1);
	}
	
	//初始化监听器
	private void initListener() {
//		storageID.getText().addKeyListener(this);
//		storageName.getText().addKeyListener(this);
		queryButton.addActionListener(this);
		addButton.addActionListener(this);
		addButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		updateButton.addActionListener(this);
		updateButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_M, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		delButton.addActionListener(this);
		delButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_D, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}
	private void setButtonEnabled(boolean flag){
		updateButton.setEnabled(flag);
		delButton.setEnabled(flag);
	}
	@Override
	public void run() {
		this.termQuery(1);
	}
	
	//条件查询
	@Override
	protected void termQuery(final int currentPage){
		final TShopMemberLevel tShopMemberLevel=new TShopMemberLevel();
//		tstore.setStoreID(storageID.getText().getText().trim());
//		tstore.setName(storageName.getText().getText().trim());
		new Thread(){
			public void run(){
				tableJsp.getViewport().remove(table);
				tableJsp.getViewport().add(waitComponent);
				try {
					pageView.setCurrentPage(currentPage);
					PageModel<TShopMemberLevel> pageModel = tShopMemberLevelService.getScrollService(pageView.getFirstResult(), pageView.getMaxResult(),tShopMemberLevel);
					pageView.setPageModel(pageModel);
					pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
					if(pageModel!=null){
						Util.clearTableRowSorter(table);
						memberLevelModel.setRowData(pageModel.getResultList());
						SwingUtilities.invokeLater(new Runnable() {
							@Override
							public void run() {
								tableJsp.getViewport().remove(waitComponent);
								tableJsp.getViewport().add(table);
								Util.setJTabelAutoClumnWidth(table);
								pagingComponent.show();
							}
						});
					}else{
						//没有订单数据
						waitComponent.setText("不存在数据");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
	@Override
	protected PageView<TShopMemberLevel> getPageView() {
		return this.pageView;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==queryButton){
			//查询按钮
			this.termQuery(1);
		}else if(e.getSource()==addButton){
			//添加按钮
			memberLevelDialog = new MemberLevelDialog(null,"添加会员级别",true);
			memberLevelDialog.showDialog(null);
			if(memberLevelDialog.isSuccess){
				this.termQuery(1);
			}
		}else if(e.getSource()==updateButton){
			//修改按钮
			this.modifyStore();
		}else if(e.getSource()==delButton){
			//删除按钮
			this.deleteStore();
		}
	}
	@Override
	public void keyReleased(KeyEvent e) {
		if(e.getSource()==table){
			if(e.getKeyCode()==KeyEvent.VK_DELETE){
				//删除按钮
				this.deleteStore();
			}
		}
//		else if(e.getSource()==storageID.getText()||e.getSource()==storageName.getText()){
//			//查询
//			if(e.getKeyCode()==KeyEvent.VK_ENTER){
//				queryButton.doClick();
//			}
//		}
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		if(e.getSource()==table){
			this.setButtonEnabled(true);
			if(table.getSelectedRowCount()!=0){
				if(e.getClickCount() == 2){
	    			//修改仓库
	    			this.modifyStore();
	    		}
			}
		}
	}
	
	//修改
	private void modifyStore(){
		if(table.getRowCount()!=0){
			int row=table.getSelectedRow();
			if(row!=-1){
				String storeID=(String)table.getValueAt(row, 0);
				try {
					TShopMemberLevel tShopMemberLevel=tShopMemberLevelService.getByIdService(storeID);
					memberLevelDialog = new MemberLevelDialog(null,"修改会员级别",true);
					memberLevelDialog.showDialog(tShopMemberLevel);
					if(memberLevelDialog.isSuccess){
						this.termQuery(1);
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}else{
				JOptionPane.showMessageDialog(null,"请选择行在进行修改");
				return;
			}
		}
	}
	 //删除
	private void deleteStore(){
		if(table.getRowCount()!=0){
			int row=table.getSelectedRow();
			if(row!=-1){
				if(JOptionPane.showConfirmDialog(null, "是否要删除选择记录")==0){
					String storeID=(String)table.getValueAt(row, 0);
					try {
						TShopMemberLevel tsm = tShopMemberLevelService.getByIdService(storeID);
						if(tsm.getTUsers().size()!=0){
							JOptionPane.showMessageDialog(null, "已存在该级别的会员，无法删除！");
							return;
						}
						tShopMemberLevelService.deleteService(storeID);
						JOptionPane.showMessageDialog(null, "删除成功");
						this.termQuery(1);
					} catch (Exception e1) {
						JOptionPane.showMessageDialog(null, "删除失败");
						e1.printStackTrace();
					}
				}
			}else{
				JOptionPane.showMessageDialog(null,"请选择行在进行删除");
				return;
			}
		}
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}
	
}
